<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2017/8/26 0026
 * Time: 上午 11:01
 */
namespace app\common\model;

use think\Db;
use think\Loader;
use think\Model;
/*
 * 站内信
 */
class UserStationLetter extends Model{
    public $stationContentDao ;
    public $userDao;
    public function initialize()
    {
        $this->stationContentDao = new StationContent();
        $this->userDao = new User();

    }

    //发送站内信
    /*
     *@param $data  ->  数组
     *@param $send_user_id  ->  发送者ID
     *@param $send_user_tatus  ->  发送者类型
     */
    public function sendMessageContent($data,$send_user_id,$send_user_tatus){
        $this->startTrans();
        try{
            $contentData['content'] = $data['content'];
            $contentData['send_time'] = time();
            $message_id=$this->stationContentDao->add($contentData);
            //处理内容分发
            //总后台
            foreach ($data['adminsSubset'] as $key=>$val){
                $letter['m_id'] = $message_id;
                $letter['send_user_tatus'] = $send_user_tatus;
                $letter['send_user_id'] = $send_user_id;
                $letter['received_user_id'] = $val;
                $letter['received_user_status'] = 0;
                $letter['is_status'] = 2;
                $letter['read_time'] = time();
                $letter['status'] = 1;
                $this->insertGetId($letter);
            }
            //区域合伙人
            foreach ($data['operationSubset'] as $key=>$val){
                $letter['m_id'] = $message_id;
                $letter['send_user_tatus'] = $send_user_tatus;
                $letter['send_user_id'] = $send_user_id;
                $letter['received_user_id'] = $val;
                $letter['received_user_status'] = 2;
                $letter['is_status'] = 2;
                $letter['read_time'] = time();
                $letter['status'] = 1;
                $this->insertGetId($letter);
            }
            //行政中心
            foreach ($data['adminSubset'] as $key=>$val){
                $letter['m_id'] = $message_id;
                $letter['send_user_tatus'] = $send_user_tatus;
                $letter['send_user_id'] = $send_user_id;
                $letter['received_user_id'] = $val;
                $letter['received_user_status'] = 1;
                $letter['is_status'] = 2;
                $letter['read_time'] = time();
                $letter['status'] = 1;
                $this->insertGetId($letter);
            }
            // 提交事务
            $this->commit();
            return true;
        } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return false;
        }
    }

    /*
     * 查询站内信
     *@param $id
     */
    public function getLetterInfo($id){
        $where['id'] = $id;
        $val = $this->alias('l')
            ->where($where)
            ->join('station_content m','l.m_id = m.message_id','left')
            ->find();
        if($val){
            if($val->send_user_tatus==0){
                $ret = $this->userDao->getOne('account,name',array('id'=>$val->send_user_id),'admin_user');
                $val->message_id = lang('HEADQUARTERS').'-'.$ret['name'];
            }
            if($val->send_user_tatus==2){
                $ret = $this->userDao->getOperationUserInfoOne('a.*,b.title,c.company_name',array('a.id'=>$val->send_user_id),'operation_user','operation_auth_group','c.company_name desc');
                $val->message_id = $ret['company_name'].'-'.$ret['title'].'-'.$ret['name'];
            }
            if($val->send_user_tatus==1){
                $ret = $this->userDao->getAdminUserInfoOne('a.*,b.title,c.company_name',array('a.id'=>$val->send_user_id),'administrative_user','administrative_auth_group','c.company_name desc');
                $val->message_id = $ret['company_name'].'-'.$ret['title'].'-'.$ret['name'];
            }
        }
        return $val;
    }

    /*
     * 删除
     *@param $id
     *@param $type 收发件标识
     */
    public function del($id,$type){
        $where['id']=$id;
        $letterInfo = $this->where($where)->find();
        if(!$letterInfo){
            return false;
        }
        $update = array();
        if($type==1){
            $update['status'] = 2;
        }else{
            $update['status'] = 3;
        }
        if($letterInfo->status==2||$letterInfo->status==3){
            $update['status'] = 0;
        }
        $ret = $this->where($where)->update($update);
        if($ret){
            return true;
        }else{
            return false;
        }
    }

    /*
     * 删除多条
     *@param $ids 数组
     *@param $type 收发件标识
     */
    public function dels($ids,$type){
        $this->startTrans();
        try{
            foreach ($ids as $key=>$val){
                $where['id']=$val;
                $letterInfo = $this->where($where)->find();
                $update = array();
                if($type==1){
                    $update['status'] = 2;
                }else{
                    $update['status'] = 3;
                }
                if($letterInfo->status==2||$letterInfo->status==3){
                    $update['status'] = 0;
                }
                $ret = $this->where($where)->update($update);
            }
            $this->commit();
            return true;
         } catch (\Exception $e) {
            // 回滚事务
            $this->rollback();
            return false;
        }

    }

    /*
     * 修改
     *@param $id
     */
    public function mod_is_status($id){
        $where['id']=$id;
        $update['is_status'] = 1;
        $ret = $this->where($where)->update($update);
        if($ret){
            return true;
        }else{
            return false;
        }
    }

    /*
     * 批量修改
     *@param $ids数组
     *@param $type 收发件标识
     */
    public function mod_is_statuss($ids,$type){
        $this->startTrans();
        try{
            foreach ($ids as $key=>$val){
                $where['id']=$val;
                $update['is_status'] = 1;
                $ret = $this->where($where)->update($update);
            }
            $this->commit();
            return true;
        }catch (\Exception $e){
            $this->rollback();
            return false;
        }
    }

}